// pages/instdetail/index.js
import urls from '../../utils/urls.js'
import loginFn from '../../utils/login.js'
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        teacherId: 0,
        showIcon: true, //显示导航图标
        tabactive: 0, //点击第几个tab
        alldetail: [], //教师详情likeFn
        haslike: false, //关注
        comments: [], //评论
        imgUrls: [],
        sharetip: false, //分享弹框
        scrolltops: 0, //页面滚动
        canscroll: false,
        from: '',
        visitorId: '',
        options: {}
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log('options:', options);
        var that = this;
        if (this.data.scrolltops == 0) {
            const query = wx.createSelectorQuery()
            query.select('#scroll_top').boundingClientRect()
            query.selectViewport().scrollOffset()
            query.exec(function(res) {
                that.setData({
                    scrolltops: res[0].top
                })
            })
        };

        var loginInfo = loginFn();
        if (options.scene) {
            // scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
            const scene = decodeURIComponent(options.scene).split('&')[0].split('=')[1]
            options.teacherId = scene
        };

        this.setData({
            from: options.from ? options.from : '',
            teacherId: options.teacherId
        });

        wx.showLoading();
        this.getTeacherInfo(() => {
            wx.hideLoading();
             //从老师或机构主页过来，统计访客
            if (that.data.from && !that.data.visitorId) {
                let details = that.data.alldetail;
                app.visitorEnter({
                    title: '老师-'+details.teacherName,
                    schoolId: details.schoolId,
                    courseId: '',
                    teacherId: details.teacherId,
                    from: that.data.from
                }, (visitorId) => {
                    that.setData({
                        visitorId: visitorId
                    });
                });
            }
        });
        
        //获取评论 
        this.getconmentFn();

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },



    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {
        console.log('onUnload')
        app.visitorLeave(this.data.visitorId, () => {
            console.log('发送访客离开时间成功');
        });
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {
        console.log('onUnload')
        app.visitorLeave(this.data.visitorId, () => {
            console.log('发送访客离开时间成功');
        });
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.getconmentFn();
        this.getTeacherInfo(() => {
            wx.stopPullDownRefresh();
        });
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },
    onPageScroll: function(e) {
        if (e.scrollTop >= this.data.scrolltops - 70) {
            this.setData({
                canscroll: true
            })
        } else {
            if (this.data.canscroll) {
                this.setData({
                    canscroll: false
                })
            }
        }
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        this.setData({
            sharetip: false
        })

        return {
            title: '这个老师挺靠谱，教学质量也挺高，你了解一下。'
        }
    },
    // 函数
    tabFn: function(e) { //tab点击
        console.log(e)
        this.setData({
            tabactive: e.currentTarget.id
        })
    },

    //点评
    reviewFn: function(e) {
        wx.navigateTo({
            url: '/pages/scoreteacher/index?teacherId=' + this.data.teacherId,
        })
    },
    likeFn: function() { //关注
        var that = this;
        var loginInfo = loginFn();
        loginInfo.teacherId = this.data.teacherId;
        wx.request({
            url: urls.focusUrl,
            data: loginInfo,
            success: function(res) {
                if (res.data.code == '0') {
                    var likes = !that.data.haslike
                    that.setData({
                        haslike: likes
                    })
                };
            }
        })
    },
    wantFn: function() { //想学
        var that = this;
        var loginInfo = loginFn()
        loginInfo.teacherId = this.data.teacherId
        wx.request({
            url: urls.teacherwant,
            data: loginInfo,
            method: 'POST',
            success: function(res) {
                console.log(res)
                if (res.data.code == '0') {
                    wx.showToast({
                        title: '操作成功',
                    })
                };
            }
        })
    },
    getlikeFn: function() { //点赞
        var that = this;
        app.request({
            url: 'teacher/like',
            data: {
                teacher_id: that.data.teacherId
            },
            sCallback:(res) => {
                if(res.code == 1) {
                    let alldetail = that.data.alldetail;
                    alldetail.likeCount = res.data.likeCount;
                    that.setData({
                        alldetail: alldetail
                    })
                    wx.navigateTo({
                        url: '/pages/reward/index?number=' + res.data.likeCount+'&teacherId='+that.data.teacherId,
                    })
                } else {
                    app.alert(res.msg);
                }
            }
        })
    },
    getconmentFn: function() { //评论列表
        var that = this;
        app.request({
            url: 'remark/listTeacher',
            data: {
                teacher_id: that.data.teacherId
            },
            sCallback: (res) => {
                if(res.code == 1) {
                    that.setData({
                        comments: res.data
                    })
                }
            }
        })
    },
    replyFn: function(e) { //回复评论
        var userInfo = wx.getStorageSync('userinfo')
        if (userInfo) {
            wx.navigateTo({
                url: '/pages/replay/index?parentRemarkId=' + e.currentTarget.dataset.id + '&teacherId=' + this.data.teacherId,
            })
        } else {
            this.setData({
                showauth: true
            })
        }

    },
    likeremarkFn: function(e) { //评论点赞
        var that = this;
        var userInfo = wx.getStorageSync('userinfo')
        if (userInfo) {
            var loginInfo = loginFn()
            loginInfo.remarkId = e.currentTarget.dataset.id
            wx.request({
                url: urls.likeTeacher,
                method: 'POST',
                data: loginInfo,
                success: function(res) {
                    if (res.data.code == '0') {
                        that.getconmentFn()
                        wx.showToast({
                            title: '点赞成功',
                        })
                    };

                }
            })
        } else {
            this.setData({
                showauth: true
            })
        }

    },

    teachermeFn: function() { //已学
        var that = this;
        var loginInfo = loginFn()
        loginInfo.teacherId = this.data.alldetail.teacherId;
        wx.request({
            url: urls.learnTeacher,
            method: 'POST',
            data: loginInfo,
            success: function(res) {
                if (res.data.code == '0') {
                    //  wx.showToast({
                    //    title: '成功',
                    //  })
                }
            }
        })
        wx.navigateTo({
            url: '/pages/scoreteacher/index?teacherId=' + this.data.alldetail.teacherId,
        })
    },
    shareFn: function() { //分享弹框
        this.setData({
            sharetip: true
        })
    },
    shareCircle: function() { //分享到朋友圈
        wx.navigateTo({
            url: '/pages/share/index?type=teacher',
        })
        this.setData({
            sharetip: false
        })
    },
    previewFn: function(e) { //预览图片
        wx.previewImage({
            current: e.currentTarget.dataset.url, // 当前显示图片的http链接
            urls: this.data.imgUrls // 需要预览的图片http链接列表
        })
    },
    showImage(e) {
        let url = e.currentTarget.dataset.url;
        wx.previewImage({
            current: url, // 当前显示图片的http链接
            urls: [url] // 需要预览的图片http链接列表
        })
    },
    //隐藏授权
    hideMask() {
        this.setData({
            sharetip: false
        })
    },
    //获取老师信息
    getTeacherInfo(callback){
        let that = this;
        app.request({
            url: 'teacher/detail',
            data: {
                teacher_id: that.data.teacherId,
            },
            sCallback:(res) => {
                if(res.code == 1) {
                    let imgUrls = [res.data.uploadImg];
                    if(res.data.teacherImages) {
                        res.data.teacherImages.forEach((item) => {
                            imgUrls.push(item.img)
                        })
                    }
                    this.setData({
                        alldetail: res.data,
                        imgUrls: imgUrls,
                        haslike: res.data.iFocus ? true : false
                    })
                    
                    callback && callback();

                    
                } else {
                    app.alert(res.msg, () => {
                        wx.navigateBack()
                    })
                }
                
            }
        })
    },
})